<?php
/**
 * Created by PhpStorm.
 * User: hc
 * Date: 2016/7/13
 * Time: 10:28
 */

namespace Home\Biz;

class AddressBiz
{
    /**
     * 生成唯一主键
     * @return string
     */
    public static function generateId()
    {
        $seq = 10000000 + CommonBiz::getNewSeq('address_id');
        return 'ADD' . $seq;
    }

    /**
     * @param $address
     */
    public static function addAddress($address)
    {
        //校验
        $msg = self::validate($address);
        if (!empty($msg)) {
            return $msg;
        }

        M('user_address')->add($address);
    }

    public static function caculateTotalPage($totalCount, $pageSize)
    {

        $count = intval($totalCount / $pageSize);

        $countLeft = $totalCount % $pageSize;

        if ($countLeft != 0) {
            $count = $count + 1;
        }

        return $count;
    }


    /**
     * 更新用户信息
     * @param $user
     */
    public static function updateAddress($address)
    {
        //校验
        $msg = self::validate($address);
        if (!empty($msg)) {
            return $msg;
        }

        M('user_address')->where('address_id="%s"', array($address['address_id']))->save($address);
    }

    /**
     * 查询
     * @param $user_id 用户ID
     * @param $consignee_name 收货人姓名
     * @param $pageSize 每页显示数量
     * @param $index 亚麻
     * @return array 结果集
     */
    public static function queryAddress($user_id, $consignee_name, $pageSize, $index)
    {
        if (!empty ($user_id)) {
            $condition_array['user_id'] = array('like', "%" . $user_id . "%");
        }

        if (!empty ($consignee_name)) {
            $condition_array['consignee_name'] = array('like', "%" . $consignee_name . "%");
        }

        $list_order_infos = M('user_address')->where($condition_array)->page($index, $pageSize)->select();

        // 总记录数
        $totalCount = M('user_address')->where($condition_array)->count();

        // 总页数
        $totalPage = self::caculateTotalPage($totalCount, $pageSize);
        // 当前页记录数
        $currentCount = $list_order_infos . $pageSize;
        // 下一页页码
        $nextIndex = $index + 1;
        // 上一页页码 如果是第一页，页码仍然为1
        $preIndex = 1;
        if ($index > 1) {
            $preIndex = $index - 1;
        }

        $resultMap = array(
            'totalCount' => $totalCount,
            'totalPage' => $totalPage,
            'currentPage' => $currentCount,
            'nextIndex' => $nextIndex,
            'preIndex' => $preIndex,
            'index' => $index,
            'datas' => $list_order_infos
        );

        return $resultMap;
    }

    /**
     * 校验地址信息
     * @param $address
     */
    public static function validate($address)
    {
        //判空    
        if (empty($address['consignee_name'])) {
            return "收货人姓名未填写";
        } else if (empty($address['user_id'])) {
            return "收货人ID未填写";
        }else if(empty($address['consignee_cellphone'])){
            return "收货人手机未填写";
        }else if(empty($address['consignee_address'])){
            return "收货人地址未填写";
        }

        //格式校验
        if(!preg_match("/^1[34578]{1}\d{9}$/",$address['consignee_cellphone'])){
           return "收货人手机格式不正确";
        }

    }

    /**
     * 根据ID 删除地址
     * @param $id
     */
    public static function deleteById($id)
    {
       $result =  M('user_address')->where('address_id= "%s"' , $id) ->delete();
        if($result == false){
            return "删除失败";
        }
    }
}